﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using DLLTest.OpertionClass;

namespace DLLTest
{
    public partial class frm_UserManage : Form
    {
        //操作类
        DataOperate dataoperateuser = new DataOperate();
        string userid = "";//id号
        //
        public frm_UserManage()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// 新增树信息
        /// </summary>
        private void AddUser()
        {
            OleDbDataAdapter sda = dataoperateuser.sqlsda("select * from UserManage where  userName='" + txtUserName.Text.Trim() + "'");
            OleDbCommandBuilder sb = new OleDbCommandBuilder(sda);
            DataSet ds = dataoperateuser.GetDataSet("select * from UserManage where  UserName='" + txtUserName.Text.Trim() + "'");
            sda.Fill(ds, "UserManage");
            if (ds.Tables[0].Rows.Count > 0)//添加一级节点
            {
                MessageBox.Show("该用户存在");                
            }
            else
            {
                MessageBox.Show("添加用户");
                try
                {
                    DataRow newrow = ds.Tables["UserManage"].NewRow();//添加一个新数据行
                    //开始添加
                    newrow["UserName"] = txtUserName.Text.Trim();
                    newrow["UserPwd"] = txtUserPwd.Text.Trim();
                    //结束添加
                    ds.Tables["UserManage"].Rows.Add(newrow);
                    sda.Update(ds, "UserManage");
                    ds.Dispose();
                    MessageBox.Show("数据新增成功！");
                }
                catch { MessageBox.Show("数据新增失败！"); }
            }
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            if (txtUserPwd.Text != txtUserPwd2.Text)
            {
               MessageBox.Show("密码不一致","提示");
            }
            else
            {
                AddUser();
                frm_UserManage_Load(null, null);
            }
        }
        /// <summary>
        /// 修改树信息
        /// </summary>
        private void EditUser(string id)
        {
            #region 修改
           
            if (txtUserPwd.Text.Trim()!= txtUserPwd2.Text.Trim())
            {
                MessageBox.Show("密码不一致！", "提示");
                return;
            }
            else
            {
                
                try
                {
                   //开始修改 
                    string updateSqlCmd = "update UserManage set UserName='" + this.txtUserName.Text.Trim() + "'"+", UserPwd='" + this.txtUserPwd.Text.Trim() + "'" + " where ID=" + userid;
                    dataoperateuser.RunSqlCommand(updateSqlCmd);
                    //结束修改  
                    //MessageBox.Show("ddd"+txtUserName.Text);                  
                    MessageBox.Show("数据修改成功！", "提示");
                }
                catch
                {
                    MessageBox.Show("用户数据修失败！", "提示");
                }
            }
        } 
        #endregion       

        private void frm_UserManage_Load(object sender, EventArgs e)
        {
            dGViewUserManage.DataSource = dataoperateuser.GetDataSet("select * from UserManage").Tables[0];           
        }

        private void dGView1_DoubleClick(object sender, EventArgs e)
        {
           userid = dGViewUserManage[0, dGViewUserManage.CurrentCell.RowIndex].Value.ToString();         
           txtUserName.Text = dGViewUserManage[1, dGViewUserManage.CurrentCell.RowIndex].Value.ToString();
           txtUserPwd.Text = dGViewUserManage[2, dGViewUserManage.CurrentCell.RowIndex].Value.ToString();
           //MessageBox.Show("sss"+userid);
        }

        private void btnEditUser_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text!= "" && txtUserName.Text!=null)
            {
                //MessageBox.Show("aaa::    " + userid);
                EditUser(userid);
                frm_UserManage_Load(null, null);
            }
            else
            {
                MessageBox.Show("请选择修改内容");
            }
         }

        private void dellUser(string id)
        {
            //MessageBox.Show("该通道可以删除", "信息提示");
            if (dataoperateuser.DellTree("UserManage", Convert.ToInt32(userid)) == true)
            {
                MessageBox.Show("删除成功", "信息提示");
                frm_UserManage_Load(null, null);       
            }
            else
            {
                MessageBox.Show("删除失败", "信息提示");
                frm_UserManage_Load(null, null); 
            }
           
        }

        private void dGView1_Click(object sender, EventArgs e)
        {
            userid = dGViewUserManage[0, dGViewUserManage.CurrentCell.RowIndex].Value.ToString();
            //MessageBox.Show(userid);
        }

        private void btnDellUser_Click(object sender, EventArgs e)
        {
            dellUser(userid);
            frm_UserManage_Load(null, null);
        }
    }
}
